package com.zhiyou100.blog.aciton.title;

import com.zhiyou100.blog.entity.PageBean;
import com.zhiyou100.blog.entity.Title;
import com.zhiyou100.blog.service.TitleService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author shkstart
 * @create 2021-10-22 20:11
 */
@WebServlet("/title/paging")
public class TitlePagingServlet extends HttpServlet {
    private TitleService service = new TitleService();


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //如果 进行查询则删除 servlet context域中的值
        //this.getServletContext().removeAttribute("titlePage");
        HttpSession session = request.getSession();
        String queryText = request.getParameter("queryText");//查询条件
        String pageNum = request.getParameter("pageNum"); //如果不传入参数默认时第一页

        if (pageNum == null) {
            pageNum = "1";
        }

        //session.removeAttribute("titlePage"); //删除域中数据
        String sessionQuery = (String) session.getAttribute("queryText");
        boolean isQuery = sessionQuery == null || "".equals(sessionQuery);
        if (isQuery) {
            if (queryText == null || "".equals(queryText)) {
                //普通查询
                System.out.println("pageNum = " + pageNum);
                PageBean<Title> titlePage = service.getListTitlePaging(Integer.parseInt(pageNum));
                System.out.println("titlePage = " + titlePage);

                session.setAttribute("titlePage", titlePage);
            } else {
                session.setAttribute("queryText", queryText);
                //条件查询
                //封装 查询
                PageBean<Title> titlePage = service.queryListTitlePagingByQustion(Integer.parseInt(pageNum), queryText);
                System.out.println("queryPage = " + titlePage);
                request.getSession().setAttribute("titlePage", titlePage);
            }
        } else {

            if (queryText == null || "".equals(queryText)) {
                System.out.println("sessionQuery ：" + sessionQuery);
                PageBean<Title> titlePage = service.queryListTitlePagingByQustion(Integer.parseInt(pageNum), sessionQuery);
                System.out.println("sessionPage = " + titlePage);
                request.getSession().setAttribute("titlePage", titlePage);
            } else {
                session.removeAttribute("queryText");
                session.setAttribute("queryText", queryText);
                //条件查询
                //封装 查询
                PageBean<Title> titlePage = service.queryListTitlePagingByQustion(Integer.parseInt(pageNum), queryText);
                System.out.println("queryPage = " + titlePage);
                request.getSession().setAttribute("titlePage", titlePage);
            }

        }


//        response.sendRedirect("/index.jsp");
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
